from rest_framework import serializers
from goods.models import SKU


class ShopCartSerializer(serializers.Serializer):
    """
    购物车序列化器
    """
    sku_id = serializers.IntegerField(label='商品　SKU ID', min_value=1)
    count = serializers.IntegerField(label='商品数量', min_value=1)
    selected = serializers.BooleanField(label='是否勾选', default=True)

    def validate(self, attrs):
        """
        对sku_id和数量进行联合校验,拿到sku_id去查找库存
        :param attrs:
        :return:
        """
        sku_id = attrs.get('sku_id')
        count = attrs.get('count')
        try:
            sku = SKU.objects.get(id=sku_id)
        except SKU.DoesNotExist:
            raise serializers.ValidationError('sku_id 不存在')
        if count > sku.stock:
            raise serializers.ValidationError('库存不足')
        return attrs